angular.module('starter.services', ['ionic'])
  //功能菜单
  .factory('Chats', function () {
    // Might use a resource here that returns a JSON array

    // Some fake testing data
    var chats = [{
      id: 2,
      name: '学生费用',
      ico: 'ion-social-yen',
      auth: []
    }, {
      id: 3,
      name: '学生课程',
      ico: 'ion-ios-bell',
      auth: []
    }, {
      id: 7,
      name: '活动报名',
      ico: 'ion-ios-paperplane',
      auth: []
    }, {
      id: 6,
      name: '教师工资',
      ico: 'ion-ios-calculator',
      auth: []
    }, {
      id: 4,
      name: '教师课程',
      ico: 'ion-ios-calendar',
      auth: []
    }, {
      id: 5,
      name: '课程登记',
      ico: 'ion-ios-compose',
      auth: []
    }, {
      id: 0,
      name: '收入查询',
      ico: 'ion-ios-download',
      auth: []
    }, {
      id: 1,
      name: '支出查询',
      ico: 'ion-ios-upload',
      auth: []
    }];

    return {
      all: function () {
        return chats;
      },
      remove: function (chat) {
        chats.splice(chats.indexOf(chat), 1);
      },
      get: function (chatId) {
        for (var i = 0; i < chats.length; i++) {
          if (chats[i].id === parseInt(chatId)) {
            return chats[i];
          }
        }
        return null;
      }
    };
  })

  //用户菜单
  .factory('UserMenu', function () {
    var menus = [{id: 0, name: '个人信息'}, {id: 1, name: '查收消息（功能未开启）'}, {id: 11, name: '修改密码'}];
    return {
      all: function (degree) {
        if (degree == 'manager') {
          return menus;
        }
      },
      getId: function (menusId) {
        for (var i = 0; i < menus.length; i++) {
          if (menus[i].id === parseInt(menusId)) {
            return menus[i];
          }
        }
        return null;
      },
      getInfo: function (degree) {
        var array = [];
        for (var i = 0; i < menus.length; i++) {
          if (menus[i].id < 10) {
            array.push(menus[i])
          }
        }
        return array
      },
      getSafe: function (degree) {
        var array = [];
        for (var i = 0; i < menus.length; i++) {
          if (menus[i].id > 10) {
            array.push(menus[i])
          }
        }
        return array
      }
    }
  })
  //用户
  //.factory('user',function($rootScope, $http){
  //  var user = $rootScope.user;
  //
  //})
  //学生
  .factory('Student', function ($rootScope, $http) {
    var user = $rootScope.user, size;
    var studentId = user.identity;
    var lessons = [], costs = [], n = 0, count = 0, income = [], incomeCount = 0, action = [];
    return {
      getLessons: function () {
        $http({
          method: 'POST',
          url: BASE_URL + 'appStudent/findLessons',
          params: {studentId: studentId},
          headers: {'Content-Type': 'application/json', 'charset': 'utf-8'}
        }).success(function (req) {
          if (req.msg) {
            lessons = req.data;
          } else {
            //console.log('Student.getLessons.error')
          }
        });
        return lessons
      },
      getCosts: function () {
        var studentId = $rootScope.user.identity, lessonId = $('#lesson :selected').attr('id');
        var startDate = $('#startDate').val(), endDate = $('#endDate').val();
        if (lessonId == 'allLesson') {
          lessonId = ''
        }
        $http({
          method: 'POST',
          url: BASE_URL + 'appStudent/findCost',
          params: {
            lessonId: lessonId,
            studentId: studentId,
            startDate: startDate,
            endDate: endDate
          },
          headers: {'Content-Type': 'application/json', 'charset': 'utf-8'}
        }).success(function (req) {
          if (req.msg) {
            costs = req.data;
          } else {
            //console.log('Student.getCosts.error')
          }
        });
        return costs
      },
      getCostsCount: function () {
        var studentId = $rootScope.user.identity, lessonId = $('#lesson :selected').attr('id');
        var startDate = $('#startDate').val(), endDate = $('#endDate').val();
        if (lessonId == 'allLesson') {
          lessonId = ''
        }
        $http({
          method: 'POST',
          url: BASE_URL + 'appStudent/findCost',
          params: {
            lessonId: lessonId,
            studentId: studentId,
            startDate: startDate,
            endDate: endDate
          },
          headers: {'Content-Type': 'application/json', 'charset': 'utf-8'}
        }).success(function (req) {
          if (req.msg) {
            count = req.count;
          } else {
            //console.log('Student.getCostsCount.error')
          }
        });
        return count
      },
      getCostsSize: function () {
        var studentId = $rootScope.user.identity, lessonId = $('#lesson :selected').attr('id');
        var startDate = $('#startDate').val(), endDate = $('#endDate').val();
        if (lessonId == 'allLesson') {
          lessonId = ''
        }
        $http({
          method: 'POST',
          url: BASE_URL + 'appStudent/findCost',
          params: {
            lessonId: lessonId,
            studentId: studentId,
            startDate: startDate,
            endDate: endDate
          },
          headers: {'Content-Type': 'application/json', 'charset': 'utf-8'}
        }).success(function (req) {
          if (req.msg) {
            size = req.sum;
          } else {
            //console.log('Student.getCostsSize.error')
          }
        });
        return size
      },
      getIncome: function () {
        var studentId = $rootScope.user.identity, actionId = $('#income-action :selected').attr('id');
        var startDate = $('#startDate').val(), endDate = $('#endDate').val(), num = $('#num').val();
        if (actionId == 'allAction') {
          actionId = ''
        }
        $http({
          method: 'POST',
          url: BASE_URL + 'appStudent/findIncome',
          params: {
            studentId: studentId,
            actionId: actionId,
            num: num,
            startDate: startDate,
            endDate: endDate
          },
          headers: {'Content-Type': 'application/json', 'charset': 'utf-8'}
        }).success(function (req) {
          if (req.msg) {
            income = req.data;
          } else {
            //console.log('Student.getIncome.error')
          }
        });
        return income
      },
      getIncomeCount: function () {
        var studentId = $rootScope.user.identity, actionId = $('#income-action :selected').attr('id');
        var startDate = $('#startDate').val(), endDate = $('#endDate').val(), num = $('#num').val();
        if (actionId == 'allAction') {
          actionId = ''
        }
        $http({
          method: 'POST',
          url: BASE_URL + 'appStudent/findIncome',
          params: {
            studentId: studentId,
            actionId: actionId,
            num: num,
            startDate: startDate,
            endDate: endDate
          },
          headers: {'Content-Type': 'application/json', 'charset': 'utf-8'}
        }).success(function (req) {
          if (req.msg) {
            incomeCount = req.count;
          } else {
            //console.log('Student.getIncomeCount.error')
          }
        });
        return incomeCount
      },
      getAction: function () {
        $http({
          method: 'POST',
          url: BASE_URL + 'appStudent/findAction',
          headers: {'Content-Type': 'application/json', 'charset': 'utf-8'}
        }).success(function (req) {
          if (req.msg) {
            action = req.data;
          } else {
            //console.log('Teacher.getAction.error')
          }
        });
        return action
      }
    }
  })
  //教师
  .factory('Teacher', function ($rootScope, $http) {
      var user = $rootScope.user, size;
      var teacherId = user.identity;
      var lessons = [], students = [], costs = [], salary = [], n = 0, count = 0;
      return {
        getLessons: function () {
          $http({
            method: 'POST',
            url: BASE_URL + 'appTeacher/findLessons',
            params: {teacherId: teacherId},
            headers: {'Content-Type': 'application/json', 'charset': 'utf-8'}
          }).success(function (req) {
            if (req.msg) {
              lessons = req.data;
              //console.log('lessons:' + lessons)
            } else {
              //console.log('Teacher.getLessons.error')
            }
          });
          return lessons
        },
        getStudents: function () {
          $http({
            method: 'POST',
            url: BASE_URL + 'appTeacher/findStudents',
            params: {teacherId: teacherId},
            headers: {'Content-Type': 'application/json', 'charset': 'utf-8'}
          }).success(function (req) {
            if (req.msg) {
              students = req.data;
              //console.log('students:' + students)
            } else {
              //console.log('Teacher.getStudents.error')
            }
          });
          return students
        },
        getCosts: function () {
          var teacherId = $rootScope.user.identity, lessonId = $('#lesson :selected').attr('id'), studentId = $('#stus :selected').attr('id');
          var startDate = $('#startDate').val(), endDate = $('#endDate').val();
          if (studentId == 'allStus') {
            studentId = ''
          }
          if (lessonId == 'allLesson') {
            lessonId = ''
          }
          $http({
            method: 'POST',
            url: BASE_URL + 'appTeacher/findCost',
            params: {
              teacherId: teacherId,
              lessonId: lessonId,
              studentId: studentId,
              startDate: startDate,
              endDate: endDate
            },
            headers: {'Content-Type': 'application/json', 'charset': 'utf-8'}
          }).success(function (req) {
            if (req.msg) {
              costs = req.data;
            } else {
              //console.log('Teacher.getCosts.error')
            }
          });
          return costs
        },
        getCostsCount: function () {
          var teacherId = $rootScope.user.identity, lessonId = $('#lesson :selected').attr('id'), studentId = $('#stus :selected').attr('id');
          var startDate = $('#startDate').val(), endDate = $('#endDate').val();
          if (studentId == 'allStus') {
            studentId = ''
          }
          if (lessonId == 'allLesson') {
            lessonId = ''
          }
          $http({
            method: 'POST',
            url: BASE_URL + 'appTeacher/findCost',
            params: {
              teacherId: teacherId,
              lessonId: lessonId,
              studentId: studentId,
              startDate: startDate,
              endDate: endDate
            },
            headers: {'Content-Type': 'application/json', 'charset': 'utf-8'}
          }).success(function (req) {
            if (req.msg) {
              count = req.count;
            } else {
              //console.log('Teacher.getCostsCount.error')
            }
          });
          return count
        },
        getCostsSize: function () {
          var teacherId = $rootScope.user.identity, lessonId = $('#lesson :selected').attr('id'), studentId = $('#stus :selected').attr('id');
          var startDate = $('#startDate').val(), endDate = $('#endDate').val();
          if (studentId == 'allStus') {
            studentId = ''
          }
          if (lessonId == 'allLesson') {
            lessonId = ''
          }
          $http({
            method: 'POST',
            url: BASE_URL + 'appTeacher/findCost',
            params: {
              teacherId: teacherId,
              lessonId: lessonId,
              studentId: studentId,
              startDate: startDate,
              endDate: endDate
            },
            headers: {'Content-Type': 'application/json', 'charset': 'utf-8'}
          }).success(function (req) {
            if (req.msg) {
              size = req.sum;
            } else {
              //console.log('Teacher.getCostsSize.error')
            }
          });
          return size
        },
        getSalaryList: function () {
          $http({
            method: 'POST',
            url: BASE_URL + 'appTeacher/findSalary',
            params: {teacherId: teacherId},
            headers: {'Content-Type': 'application/json', 'charset': 'utf-8'}
          }).success(function (req) {
            if (req.msg) {
              salary = req.data
            } else {
              //console.log('Teacher.getSalaryList.error')
            }
          });
          return salary
        },
        getSalaryCount: function () {
          $http({
            method: 'POST',
            url: BASE_URL + 'appTeacher/findSalary',
            params: {teacherId: teacherId},
            headers: {'Content-Type': 'application/json', 'charset': 'utf-8'}
          }).success(function (req) {
            if (req.msg) {
              n = req.count
            } else {
              //console.log('Teacher.getSalaryCount.error')
            }
          });
          return n
        }
        //,
        //getStuByLesson: function (lessonId) {
        //  $http({
        //    method: 'POST',
        //    url: BASE_URL + 'appTeacher/findStuByLesson?teacherId=' + teacherId + '&lessonId=' + lessonId,
        //    headers: {'Content-Type': 'application/json', 'charset': 'utf-8'}
        //  }).success(function (req) {
        //    if (req.msg) {
        //      students = req.data;
        //      console.log('students:' + students)
        //    } else {
        //      console.log('Teacher.getStudents.error')
        //    }
        //  });
        //  return students
        //}
      }
    }
  );

